package com.qf.news.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.news.dao.NewsTypeDao;
import com.qf.news.entity.NewsType;
import com.qf.news.exception.ServiceException;
import com.qf.news.service.NewsTypeService;
import org.springframework.stereotype.Service;

/**
 * 新闻分类(NewsType)表服务实现类
 *
 * @author makejava
 * @since 2023-07-20 10:11:39
 */
@Service("newsTypeService")
public class NewsTypeServiceImpl extends ServiceImpl<NewsTypeDao, NewsType> implements NewsTypeService {


    @Override
    public boolean save(NewsType entity) {

        //判断当前分类名称是否已经存在
        Long count = this.query()
                .eq("t_name", entity.getTName())
                .count();

        if (count > 0) {
            //分类已经存在
            throw new ServiceException(401, "分类名称已经存在！");
        }

        return super.save(entity);
    }

    /**
     * 根据分类id，修改分类下的新闻数量
     *
     * update news_type set t_count = t_count + #{count} where id = #{tid}
     *
     * @param tid
     * @param count
     * @return
     */
    @Override
    public int updateNewsTypeCount(Integer tid, Integer count) {
        this.update()
                .setSql("t_count = t_count + " + count)
                .eq("id", tid)
                .update();
        return 1;
    }
}

